﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Benefit.Models.Sys;
namespace Benefit.Service.Day
{
    /// <summary>
    /// 项目分时数据
    /// </summary>
    public class ServiceDayTradeInfo : Benefit.Interface.Day.IDayTradeInfo
    {
        Benefit.DB.DBManager db = null;
        public ServiceDayTradeInfo(Benefit.DB.DBManager db)
        {
            this.db = db;
        }


        /// <summary>
        /// 删除当日分时记录
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        public void Delete(int tradeHistoryId)
        {
            var query = from t in db.DayTradeInfo where t.TradeHistoryId == tradeHistoryId select t;
            foreach (Models.Day.DayTradeInfo c in query)
            {
                db.DayTradeInfo.Remove(c);
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 保存账号分时数据OK
        /// </summary>
        /// <param name="tradeHistoryId">插入时间编号</param>
        /// <param name="pdate">插入时间</param>
        public void InitDayStateChange(int tradeHistoryId, string pdate)
        {
            Interface.ServerData.IT_Account ita = new ServerData.ServiceT_Account(db);
            List<Models.ServerData.T_Account> serverAccounts = ita.GetList();
            Interface.Sys.IAccount iacount = new Sys.ServiceAccount(db);
            Interface.ServerData.IT_TradeDetail_History h = new ServerData.ServiceT_TradeDetail_History(db);
            //  Sys.Account _account = new Sys.Account();
            //  Data.ServerData.T_TradeDetail_History h = new ServerData.T_TradeDetail_History();
            foreach (Models.ServerData.T_Account account in serverAccounts)
            {
                for (int i = 0; i < 8; i++)//这个i就是阶段的代号
                {
                    Models.Day.DayTradeInfo info = new Models.Day.DayTradeInfo();
                    info.AccountId = iacount.GetAccountId(account.LoginAccount);
                    info.State = i;
                    info.BillCount = h.GetStateDayBillCount(pdate, account.Id, i);
                    info.DayCount = h.GetDayStateCloseProfit(pdate, account.Id, i) - h.GetDayStateCommission(pdate, account.Id, i);
                    info.Free = h.GetDayStateCommission(pdate, account.Id, i);
                    info.LoseCount = h.GetDayStateLoseCount(pdate, account.Id, i);
                    info.Profit = h.GetDayStateCloseProfit(pdate, account.Id, i);
                    info.SumLost = h.GetStateDayAllLose(pdate, account.Id, i);
                    info.SumWin = h.GetStateDayAllWin(pdate, account.Id, i);
                    info.TradeHistoryId = tradeHistoryId;
                    info.UsedMargin = h.GetDayStateMargin(pdate, account.Id, i);
                    info.WinCount = h.GetStateDayWinCount(pdate, account.Id, i);
                    db.DayTradeInfo.Add(info);
                }
            }
            db.SaveChanges();
        }

        /// <summary>
        /// 查询账户某天的分时交易信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="accountid"></param>
        /// <returns></returns>
        public List<Models.Day.DayTradeInfo> GetAccountDayTradeInfos(int tradeHistoryId, int accountid)
        {
            List<Models.Day.DayTradeInfo> infos = new List<Models.Day.DayTradeInfo>();
            var query = db.DayTradeInfo.Where(a => a.AccountId == accountid).Where(a => a.TradeHistoryId == tradeHistoryId);
            if (query.Count() > 0)
            {
                infos = query.ToList();
            }
            return infos;
        }
        /// <summary>
        /// 查询账户某天的分时交易信息
        /// </summary>
        /// <param name="tradeHistoryId"></param>
        /// <param name="accountid"></param>
        /// <returns></returns>
        public List<Models.Day.DayTradeInfo> GetAccountDayTradeInfos(int tradeHistoryId, int accountid, bool win)
        {
            List<Models.Day.DayTradeInfo> infos = new List<Models.Day.DayTradeInfo>();
            if (win)
            {
                infos = db.DayTradeInfo.Where(a => a.AccountId == accountid).Where(a => a.Profit > 0).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            }
            else
            {
                infos = db.DayTradeInfo.Where(a => a.AccountId == accountid).Where(a => a.Profit <= 0).Where(a => a.TradeHistoryId == tradeHistoryId).ToList();
            }


            return infos;
        }
    }
}